﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;

using NUnit.Framework;
using System.Web.Mvc;

using Milo.Framework;

namespace Milo.Tests
{
    [TestFixture]
    public class ActionMethodInfoTests
    {
        [Test]
        public void NoParmsAction()
        {
            ActionMethodInfo ami = GetAmi(c => c.NoParms());
            ami.Ensure();

            Assert.AreEqual(ami.ActionName, "NoParms");
            Assert.IsFalse(ami.IsIdMethod);
            Assert.AreEqual(0, ami.Parameters.Count);
        }

        [Test]
        public void IdParmAction()
        {
            ActionMethodInfo ami = GetAmi(c => c.IdParm(12));
            ami.Ensure();

            Assert.IsTrue(ami.IsIdMethod);
            Assert.AreEqual(12, ami.Id);

            Assert.AreEqual(12, ami.Parameters["id"]);
        }

        [Test]
        public void OneParmAction()
        {
            ActionMethodInfo ami = GetAmi(c => c.OneParm("one"));
            ami.Ensure();

            Assert.IsFalse(ami.IsIdMethod);
            Assert.AreEqual(1, ami.Parameters.Count);

            Assert.AreEqual("one", ami.Parameters["one"]);
        }

        [Test]
        public void ThreeParmsAction()
        {
            ActionMethodInfo ami = GetAmi(c => c.ThreeParms("one", 2, "three"));
            ami.Ensure();

            Assert.IsFalse(ami.IsIdMethod);
            Assert.AreEqual(3, ami.Parameters.Count);

            Assert.AreEqual("one", ami.Parameters["one"]);
            Assert.AreEqual(2, ami.Parameters["two"]);
            Assert.AreEqual("three", ami.Parameters["three"]);
        }

        [Test]
        public void AliasedParmAction()
        {
            ActionMethodInfo ami = GetAmi(c => c.ActionToAlias());
            ami.Ensure();

            Assert.AreEqual(ami.ActionName, "Different");
            Assert.IsFalse(ami.IsIdMethod);
            Assert.AreEqual(0, ami.Parameters.Count);
        }

        private ActionMethodInfo GetAmi(Expression<Func<ActionMethodInfoTests, ActionResult>> action)
        {
            return new ActionMethodInfo(action);
        }

        #region actions
        public ActionResult NoParms()
        {
            return null;
        }
        public ActionResult IdParm(int id)
        {
            return null;
        }
        public ActionResult OneParm(string one)
        {
            return null;
        }
        public ActionResult ThreeParms(string one, int two, string three)
        {
            return null;
        }
        [System.Web.Mvc.ActionName("Different")]
        public ActionResult ActionToAlias()
        {
            return null;
        }
        #endregion
    }
}
